"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var moment = require("moment");
var http_1 = require("@angular/http");
var api_1 = require("config/api");
var OauthService = /** @class */ (function () {
    function OauthService(http, router, localStorageService) {
        this.http = http;
        this.router = router;
        this.localStorageService = localStorageService;
    }
    OauthService.prototype.doLogin = function (username, password) {
        var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
        var options = new http_1.RequestOptions({ headers: headers });
        var body = JSON.stringify({ account: username, password: password });
        return this.http.post(api_1.adSystemApiUrl + '/v1/member/auth/login', body, options).map(function (response) { return response.json(); });
    };
    OauthService.prototype.doAdminLogin = function (username, password) {
        var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
        var options = new http_1.RequestOptions({ headers: headers });
        var body = JSON.stringify({ account: username, password: password });
        return this.http.post(api_1.adSystemApiUrl + '/v1/admin/auth/login', body, options).map(function (response) { return response.json(); });
    };
    // 刷新
    OauthService.prototype.doRefresh = function () {
        var _this = this;
        var auth, type;
        if (!!this.localStorageService.getUserInfo()) {
            auth = this.localStorageService.getUserInfo();
            type = 'member';
        }
        else if (!!this.localStorageService.getAdminInfo()) {
            auth = this.localStorageService.getAdminInfo();
            type = 'admin';
        }
        else {
            return false;
        }
        // 创建时间
        var time = parseInt(auth.time, 10);
        // 过期时间
        var expireTime = time + 3600;
        // 当前时间
        var nowTime = parseInt(moment().format('X'), 10);
        // 过期
        if (expireTime < nowTime) {
            alert('登录信息过期，请重新登录');
            this.clear();
            // this.router.navigate(['/home/index']);
            location.href = '/';
            return false;
        }
        if ((expireTime - 600) <= nowTime && nowTime <= (expireTime - 200)) {
            if (type == 'member') {
                this.getMemberToken().subscribe(function (res) {
                    if (res.code === 2000) {
                        auth.time = nowTime;
                        _this.localStorageService.setUserInfo(auth);
                        _this.localStorageService.setToken(res.data);
                    }
                });
            }
            else {
                this.getAdminToken().subscribe(function (res) {
                    if (res.code === 2000) {
                        auth.time = nowTime;
                        _this.localStorageService.setAdminInfo(auth);
                        _this.localStorageService.setToken(res.data);
                    }
                });
            }
        }
        return false;
    };
    // 获取access token
    OauthService.prototype.getAccessToken = function () {
        var auth = this.localStorageService.getToken();
        if (auth) {
            return auth.access_token;
        }
        return '';
        /*if (!auth) {
         return '';
         }
         // 创建时间
         const time = parseInt(auth.time, 10);o
         // 过期时间
         const expireTime = time + 3600;
         // 当前时间
         const nowTime = parseInt(moment().format('X'), 10);
         // 过期
         if (expireTime < nowTime) {
         console.log('过期');
         this.clear();
         return '';
         }
         console.log(auth);
         return auth.access_token;*/
    };
    // 退出登录
    OauthService.prototype.clear = function () {
        localStorage.clear();
        // this.router.navigate(['/home/index']);
        location.href = '/';
    };
    // 是否登录
    OauthService.prototype.isLogin = function () {
        var token = this.getAccessToken();
        return token && token != null;
    };
    // 获取登录的用户信息
    // getUser() {
    //   return JSON.parse(localStorage.getItem('user'));
    // }
    OauthService.prototype.getMemberToken = function () {
        var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
        var options = new http_1.RequestOptions({ headers: headers });
        var token = this.localStorageService.getToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token.access_token);
        }
        return this.http.get(api_1.adSystemApiUrl + '/v1/member/auth/refresh', options).map(function (response) { return response.json(); });
    };
    OauthService.prototype.getAdminToken = function () {
        var headers = new http_1.Headers({ 'Content-Type': 'application/json' });
        var options = new http_1.RequestOptions({ headers: headers });
        var token = this.localStorageService.getToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token.access_token);
        }
        return this.http.get(api_1.adSystemApiUrl + '/v1/admin/auth/refresh', options).map(function (response) { return response.json(); });
    };
    OauthService = __decorate([
        core_1.Injectable()
    ], OauthService);
    return OauthService;
}());
exports.OauthService = OauthService;
